package com.yc.damaimaven.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yc.damaimaven.entity.User;
import com.yc.damaimaven.mapper.UserMapper;
import com.yc.damaimaven.service.BizException;
import com.yc.damaimaven.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xw
 * @since 2024-07-09
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    @Transactional
    public User login(User user) throws BizException{
        //输入是否正确
        if(user.getUsername()==null || user.getUsername().isBlank()){
            throw new BizException("请输入用户名！");
        }
        if(user.getPassword()==null || user.getPassword().isBlank()){
            throw new BizException("请输入密码！");
        }
        if(user.getPassword().length()<6){
            throw new BizException("密码长度必须大于等于6！");
        }
        //TODO 请自行扩展其他判断

        //数据是否正确
        //LambdaQueryWrapper<User>lqw = new LambdaQueryWrapper<>();
        //lqw.eq(User::getUsername,user.getUsername());
        //if(this.count(lqw)>0){
        //    throw new BizException("该用户名已被注册！");
        //}

        LambdaQueryWrapper<User>lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername,user.getUsername());
        lqw.eq(User::getPassword,user.getPassword());
        User one=getOne(lqw);
        if(one==null){
            throw new BizException("用户名或密码错误!");
        }
        //返回用户对象
        return one;
        //返回用户对象
        //写入数据库
        //save(user);


    }

}
